<template>
  <basic-container>
    <XmTable :config="tableConfig" :noWatchParams="noWatchParams" class="table" ref="table">
      <div slot="filter">
        <el-form class="fl" :inline="true" ref="form" :model="noWatchParams">
          <el-form-item label="司机名称">
            <el-input v-model.trim="noWatchParams.account" size="small" placeholder="输入全名" clearable></el-input>
          </el-form-item>
          <el-form-item label="城市">
            <CitySelect ref="citySelect" v-model="deptId" size="small" placeholder="城市" clearable filterable></CitySelect>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="search" size="small" icon="el-icon-search" style="margin-left: 10px;">搜索
            </el-button>
            <el-button @click="reset" size="small" icon="el-icon-delete" style="margin-left: 10px;">清空</el-button>
          </el-form-item>
        </el-form>
        <div class="fr">
          <el-button icon="el-icon-plus" size="small" type="danger" @click="add">新增</el-button>
        </div>
      </div>

      <template slot="column-warehouses" slot-scope="{data}">
        <div v-if="data.warehouses && data.warehouses.length">
          <el-tag class="mr-5 mb-5" v-for="item in data.warehouses">
            {{item.name}}
          </el-tag>
        </div>
      </template>
      <template slot="column-type" slot-scope="{data}">
        <span>{{addedServicesTypeMap[data.type]}}</span>
      </template>
      <template slot="column-operate" slot-scope="{data}">
        <el-button class="ml-0 mr-10" type="text" icon="el-icon-edit" size="small" @click="edit(data)">
          编辑
        </el-button>
        <el-button class="ml-0 mr-10" type="text" icon="el-icon-view" size="small" @click="view(data)">
          查看
        </el-button>
        <el-button class="ml-0 mr-10" type="text" icon="el-icon-delete" size="small" @click="remove(data)">
          删除
        </el-button>
      </template>
    </XmTable>

    <addDialog ref="addDialog" @submit="search"></addDialog>
  </basic-container>
</template>

<script>
import { bindDriverWarehouse } from '@/api/transfer'
import tableConfig from './config/table'
import addDialog from './addDialog'
export default {
  data() {
    return {
      tableConfig,
      deptId: '',
      noWatchParams: {
        account: '',
        deptName: '',
      },
    };
  },
  methods: {
    fetch() {
      this.noWatchParams.deptName = this.$refs.citySelect && this.$refs.citySelect.getSelectedLabel()
      this.$refs.table.fetch()
    },
    search() {
      this.fetch()
    },
    reset() {
      this.deptId = ''
      this.noWatchParams = {
        account: '',
        deptName: '',
      }
      this.$nextTick(() => {
        this.fetch()
      })
    },
    add() {
      this.$refs.addDialog.show({ type: 'add' })
    },
    edit(data) {
      this.$refs.addDialog.show({ type: 'edit', data})
    },
    view(data) {
      this.$refs.addDialog.show({ type: 'view', data})
    },
    remove(data) {
      this.$confirm('请确认是否删除该记录?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        bindDriverWarehouse({
          "userId": data.id,
          "warehouseIds": [],
        }).then(res => {
          if (res.data.code === 200) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.search()
          } else {
            this.$message({
              type: 'error',
              message: res.data.msg
            });
          }
        })
      })
    },
  },
  components: {
    addDialog
  }
};
</script>
<style lang="scss" scoped>

</style>